import { useEffect, useState } from 'react'
import { useNavigate } from 'react-router-dom'
import { pageType, IMessage, tableInterface, platformInstructions } from '../interface/interface'
import { messagePushList, getPlatformSetList } from '../service/service'

export default (): IMessage => {
  const routerPush = useNavigate()

  // 特别注意表格数据
  const [paySpecialAttentionToTableData, setPaySpecialAttentionToTableData] = useState<tableInterface[]>([])

  // tabs 受控
  const [tabsActive, setTabsActive] = useState<string>('4')

  // 分页信息
  const [pageParams, setPageParams] = useState<pageType>({
    totalCount: 0,
    pageIndex: 1,
    pageSize: 10,
  })

  // 平台概况和操作指南列表数据
  const [platformInstructionsData, setPlatformInstructionsData] = useState<platformInstructions[]>([])

  // 更换页码
  const handleChangePage = async (page: number, pageSize?: number) => {
    await setPageParams((v) => ({
      ...v,
      pageIndex: page,
      pageSize: pageSize || pageParams.pageSize,
    }))
  }

  // 获取消息推送列表
  const getMessageList = async () => {
    const params = {
      pageSize: pageParams.pageSize,
      pageIndex: pageParams.pageIndex,
      search: {
        type: '',
        title: '',
        content: '',
      },
      searchCount: true,
    }
    const res = await messagePushList(params)
    if (res && res.code === 200) {
      setPageParams({ ...pageParams, totalCount: res.data.totalCount })
      setPaySpecialAttentionToTableData(res.data.items)
    }
  }

  // 获取平台概况，操作指南底下的数据
  const getPlatformSetListData = async () => {
    const params = {
      pageSize: pageParams.pageSize,
      pageIndex: pageParams.pageIndex,
      search: {
        type: tabsActive,
      },
      searchCount: true,
    }
    const res = await getPlatformSetList(params)
    if (res && res.code === 200) {
      setPlatformInstructionsData(res.data.items)
    }
    // console.log('我是获取到的平台概况和操作指南', res.data.items)
  }

  // 切换 tabs 事件
  const changeTabs = (value: string) => {
    setTabsActive(value)
  }

  // 点击查看 跳转到查看页面
  const examinePushRouter = (record: any) => {
    routerPush('/message-center/MessageExamine', { state: record })
  }

  // 页码更新重新请求表格数据
  useEffect(() => {
    getMessageList()
  }, [pageParams.pageIndex, pageParams.pageSize])

  useEffect(() => {
    getPlatformSetListData()
  }, [tabsActive])

  return {
    examinePushRouter,
    pageParams,
    handleChangePage,
    paySpecialAttentionToTableData,
    changeTabs,
    tabsActive,
    platformInstructionsData,
  }
}
